﻿using System;
using System.ComponentModel;
using System.Drawing;

using ExtensibleEventViewer.Console.EventArguments;


namespace ExtensibleEventViewer.Console.Controls
{
	public partial class AddComputerControl : UserControlBase
	{
		public event EventHandler<ComputerEventArgs> AddComputer;
		public event EventHandler CancelAdding;

		public AddComputerControl()
		{
			InitializeComponent();

			txtComputerName.Validating += AddComputerControlValidate;
		}

		private void AddComputerControlValidate(object sender, CancelEventArgs e)
		{
			if (string.IsNullOrEmpty(txtComputerName.Text))
			{
				txtComputerName.BackColor = ERROR_COLOR;

				e.Cancel = true;

				return;
			}

			e.Cancel = false;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (AddComputer != null)
			{
				ComputerEventArgs eventArgs = new ComputerEventArgs
				                              	{
				                              		ComputerName = txtComputerName.Text
				                              	};

				AddComputer(this, eventArgs);
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			if (CancelAdding != null)
			{
				CancelAdding(sender, e);
			}
		}

		private void txtComputerName_TextChanged(object sender, EventArgs e)
		{
			if(txtComputerName.BackColor == ERROR_COLOR)
			{
				txtComputerName.BackColor = Color.White;
			}
		}
	}
}
